﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using Water.Model;
using WaterApp.Common;

namespace WaterApp.QueryReport
{
    public partial class MeterReadingPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMeterReading();
            }
        }

        public void BindMeterReading()
        {
            string url = WebapiUrl.weburl() + "GetMeterReadings";
            MeterReadCondition mrc = new MeterReadCondition()
            {
                custid = tbaccount.Text,
                mreaddt = tbcbdt.Text,
                address = tbaddress.Text,
                custname = tbhuname.Text,
                phone = tbtelphone.Text
            };
            var requestJson = JsonConvert.SerializeObject(mrc);
            HttpContent httpContent = new StringContent(requestJson);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;
            IList<MeterReading> mr = JsonConvert.DeserializeObject<IList<MeterReading>>(responseJson);
            if (mr == null)
            {
                lbmessage.Text = "没有找到相关记录！请重新输入查询条件！";
                return;
            }
            GvMeterRead.DataSource = mr;
            GvMeterRead.DataBind();
            lbmessage.Text = "";
        }

        protected void meterreadfound_Click(object sender, EventArgs e)
        {
            BindMeterReading();
        }

        protected void GvMeterRead_SelectedIndexChanged(object sender, EventArgs e)
        {
            string mrno = GvMeterRead.SelectedRow.Cells[0].Text;
            string epcno = GvMeterRead.SelectedRow.Cells[2].Text;
            string chaom = GvMeterRead.SelectedRow.Cells[3].Text;
            string yshul = GvMeterRead.SelectedRow.Cells[4].Text;
            string url = WebapiUrl.weburl() + "GetMeterByEpcNo/" + epcno;

            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            IList<Meter> mr = JsonConvert.DeserializeObject<IList<Meter>>(responseJson);
            StringBuilder strbul = new StringBuilder();
            foreach (Meter me in mr)
            {
                hfmrno.Value = mrno;
                strbul.Append("<table style='width:100%;'><tr>");
                strbul.Append("<td style='text-align:right; font-weight:bold;'>帐号:</td><td style='padding:0px 0px 0px 10px;text-decoration:underline;'>" + me.CUSTID + "</td></tr>");
                strbul.Append("<tr><td style='text-align:right; font-weight:bold;'>地址:</td><td style='padding:0px 0px 0px 10px;text-decoration:underline;'>" + me.LOCATION + "</td></tr>");
                strbul.Append("<tr><td style='text-align:right; font-weight:bold;'>户名:</td><td style='padding:0px 0px 0px 10px;text-decoration:underline;'>" + me.CUSTNAME + "</td></tr>");
                strbul.Append("<tr><td style='text-align:right; font-weight:bold;'>休息日:</td><td style='padding:0px 0px 0px 10px;text-decoration:underline;'>" + me.OFFDAY + "</td></tr>");
                strbul.Append("<tr><td style='text-align:right; font-weight:bold;'>电话:</td><td style='padding:0px 0px 0px 10px;text-decoration:underline;'>" + me.PHONE.ToString() + "</td></tr>");
                strbul.Append("<tr><td style='text-align:right; font-weight:bold;'>抄码:</td><td style='padding:0px 0px 0px 10px;text-decoration:underline;'>" + chaom + "&nbsp;&nbsp;<b>用水量(立方米):</b>&nbsp;&nbsp;" + yshul + "</td></tr>");
                strbul.Append("</table>");
            }
            tbbase.InnerHtml = strbul.ToString();
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            string url = WebapiUrl.weburl() + "AddCheckMemter";
            CheckMemter cm = new CheckMemter()
            {
                MRNO = hfmrno.Value,
                CheckMeter = dplfh.SelectedValue,
                CheckMemo = tbfhadv.Text,
                CheckEmp = Membership.GetUser().UserName
            };
            var requestJson = JsonConvert.SerializeObject(cm);
            HttpContent httpContent = new StringContent(requestJson);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync(url,httpContent).Result.Content.ReadAsStringAsync().Result;
        }
    }
}